<template>
  <div>

    <div class="detail_title">
      <h4 v-text="pageName"></h4>
      <a class="close" @click="close" href="javascript:void(0)">×</a>
    </div>

    <div class="var-card-info-content">
      <h4 class="var_bgColor">级别信息</h4>
      <div class="content" v-cloak>
        <Row>

          <Col span="6">
          <label>上级级别</label>
          <Select size="small" style="width: 50%" v-model="inputInfo.parentLevel" filterable>
            <Option v-for="item in levelDir" v-text="item.levelName" v-bind:value="item.id"></Option>
          </Select>
          </Col>

          <Col span="6">
          <label>级别名称</label>
          <Input size="small" style="width: 50%" v-model="inputInfo.levelName"> </Input>
          </Col>

          <Col span="6">
          <label>状态</label>
          <Select size="small" style="width: 50%" v-model="inputInfo.state">
            <Option v-for="(item,index) in state" v-bind:value="index" v-text="item"></Option>
          </Select>
          </Col>

        </Row>

      </div>
    </div>

    <div class="bottom_btn_div">

      <Button size="small" @click="saveInfo" type="primary">提交</Button>
      <Button size="small" v-if="levelId==='-1'" @click="resetInfo" type="error">重 置</Button>
      <Button size="small" @click="close">关 闭</Button>

    </div>

  </div>
</template>

<script type="text/javascript">
  import api from '../../utils/api.js';
  import commonCode from '../../assets/json/common_code.json';
  export default {
    data: function () {
      return {
        state: commonCode.commonState,

        inputInfo: {
          levelName: '',
          parentLevel: '',
          state: '',
          partnerId: JSON.parse(localStorage.Manage).USER_PARTNER_PRIVILEGE.id,
        },

        levelId: '',
        levelName: '',

        pageName: '',

        levelDir: [],

      }
    },
    created: function () {
      this.levelId = this.$route.params.id;
      this.levelName = this.$route.params.name;

      api.getSalemanLevelDir(this.inputInfo.partnerId, "1").then(res => {

        this.levelDir = res.body.data;
        this.levelDir.push({
          id: '',
          levelName: ''
        });

        if (this.levelId !== '-1') {

          this.pageName = '修改业务员级别';

          this.getSaleLevel();

        } else {
          this.pageName = '新增业务员级别';
        }


      })
    },
    methods: {

      getSaleLevel: function () {
        api.getSaleLevelDetailInfo(this.levelId, this.levelName).then(res => {

          this.inputInfo = res.body.data;

        })
      },

      saveInfo: function () {

        if (this.levelId === '-1') {
          api.saveSaleLevel(this.inputInfo).then((res) => {

            this.changeInfoFinish(res);

          })
        } else {
          api.updateSaleLevelInfo(this.inputInfo).then((res) => {

            this.changeInfoFinish(res);

          })
        }


      },
      getSaleLevelList: function () {

        api.getSalemanLevelDir(this.inputInfo.partnerId, "1").then(res => {

          this.levelDir = res.body.data;
          this.levelDir.push({
            id: '',
            levelName: ''
          });

        })
      },
      changeInfoFinish: function (res) {
        alert(res.body.message.info);
        this.getSaleLevelList();
      },

      resetInfo: function () {
        this.inputInfo = {
          parentLevel: "",
          teamName: "",
          state: "",
          partnerId: JSON.parse(localStorage.Manage).USER_PARTNER_PRIVILEGE.id,
        };
      },

      close: function () {
        this.$router.go(-1);
      }

    },
  }
</script>
